/**
 * 
 */
package com.m7012e.a_remote.ar_listeners.network.protocol;

import com.leoj.utils.Handler;
import com.leoj.utils.tcp_network.ApplicationProtocol;
import com.leoj.utils.tcp_network.ApplicationProtocolFactory;
import com.m7012e.a_remote.ar_listeners.network.protocol.internEvents.CommunicationOver;

/**
 * @author leoj
 *
 */
public class DaemonProtocolFactory implements ApplicationProtocolFactory, Handler<CommunicationOver> {
	private boolean _available;
	/**
	 * 
	 */
	public DaemonProtocolFactory() {
		_available = true;
	}
	
	/* (non-Javadoc)
	 * @see com.leoj.utils.tcp_network.ApplicationProtocolFactory#createProtocol()
	 */
	@Override
	public ApplicationProtocol createProtocol() {
		System.out.println("DaemonProtocolFactory is creating a new Protocol");
		if(_available){
			_available = false;
			return new DaemonDisplayProtocol(this);
		}else{
			return new DaemonBusyProtocol();
		}
	}

	/* (non-Javadoc)
	 * @see com.leoj.utils.Handler#handle(java.lang.Object)
	 */
	@Override
	public void handle(CommunicationOver args) {
		System.out.println("the communication is over ! the daemon can accept a new DisplayRequest");
		_available = true;
	}
	public boolean isAvailable(){ return _available; }
}
